package com.atjava.webspider.thread;

import java.io.File;

import com.atjava.jox.io.JOXReader;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.thread.infs.NamedTask;

public class TestThread implements NamedTask{
	
	private String name;
	private boolean started;
	
	public TestThread(String name){
		this.name = name;
	}
	
	public void getName1(){
		synchronized(name){
			for (int i = 0; i < 50; i++) {
				System.out.println("========name : " + name);
			}
		}
	}
	
	public static void main1(String[] args) throws InterruptedException {
		
		TestThread tt = new TestThread("a");
		Thread t = new Thread(tt);
		t.start();
		Thread.sleep(1000);
		tt.pause();
		Thread.sleep(1000);
		tt.goon();
		Thread.sleep(1000);
		tt.stop();
		
		
	}
	
	public static void main(String[] args)throws Exception{
		
		JOXReader jox = new JOXReader();
		File f = new File("D:\\wkspace\\WebSpider\\tasks\\task-20110123023232.xml");
		Task t = jox.readXml(f, Task.class);
		Runnable r = new SpiderTask(t); 
		new Thread(r).start();
		
	}
	
	public void run(){
		started = true;
		print2();
	}
	public void stop() {
		started = false;
		synchronized (name) {
			name.notify();
		}
		System.out.println("========stopped : " + name);
	}
	
	public void waitting(){}

	public void print1(){
		synchronized(name){
			for (int i = 0; i < 50; i++) {
				System.out.println(name + "====1=====" + i);
				name = name + i;
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private boolean paused;
	
	public void pause(){
		paused = true;
	}
	
	public void goon(){
		paused = false;
		synchronized (name) {
			name.notify();
		}
	}
	
	public void print2(){
		for (int i = 0; i < 30; i++) {
			synchronized (name) {
				if(paused){
					try {
						name.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			System.out.println(name + "====2=====" + i);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(!started)
				break;
		}
	}
	public synchronized void print3(){
		for (int i = 0; i < 50; i++) {
			System.out.println(name + "====3=====" + i);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public Object getName() {
		return this.name;
	}

}
